package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.Gglhd;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.GglhdService;
import net.starwifi.duobao.service.GgljpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller - member.
 *
 * @author ruoo
 * @version 1.0
 */
@Controller("admingglhdController")
@RequestMapping("/gglhd")
public class GglhdController extends BaseController {

    @Autowired
    GglhdService gglhdService;
    @Autowired
    GgljpService ggljpService;
//    @Autowired
//    GglhdRepository gglhdRepository;
    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/gglhd/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Gglhd gglhd, RedirectAttributes redirectAttributes) {
        if (!isValid(gglhd)) {
            return ERROR_VIEW;
        }
        gglhd.setStatus(Integer.bitCount(0));
        gglhdService.save(gglhd);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }


    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("entity", gglhdService.find(id));
        return "/gglhd/edit";
    }


    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Gglhd gglhd, RedirectAttributes redirectAttributes) {
        Gglhd pGglhd=gglhdService.find(gglhd.getId());
        gglhdService.update(gglhd);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, ModelMap model,HttpServletRequest request) {
        pageView.filterQuery(request);
        pageView.addOrder(Order.desc("id"));
        model.addAttribute("page", gglhdService.getScrollData(pageView));
        return "/gglhd/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        gglhdService.delete(ids);
        return SUCCESS_MESSAGE;
    }

    /**
     * 开启活动
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/OnGgl", method = RequestMethod.GET)
    public String OnGgl(Long id, RedirectAttributes redirectAttributes) {
        int status =1;
//        gglhdRepository.OnGgl(id,status);
        gglhdService.OnGgl(id,status);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 关闭活动
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/OffGgl", method = RequestMethod.GET)
    public String OffGgl(Long id, RedirectAttributes redirectAttributes) {
        int status=0;
        gglhdService.OffGgl(id,status);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

}
